package sg.edu.nus.iss.jass.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import sg.edu.nus.iss.jass.controller.MainMenuController;

/**
 * @version 1.0
 * @author SE19 Team 1
 * 
 */
public class JASSWindow extends JFrame {
	private static final long serialVersionUID = 1L;

	private MainMenuController mainMenuController;
	private JComponent currComponent;
	private JPanel menuPanel, bodyPanel;
	
	// Constructor.
	public JASSWindow(MainMenuController mainController) {
		super("Job Advertisement and Search System (JASS)");
		this.mainMenuController = mainController;
		
		bodyPanel = initBodyPanel();
		menuPanel = initHeaderPanel();
		Container container = this.getContentPane();
		container.add(menuPanel, BorderLayout.NORTH);
		container.add(bodyPanel, BorderLayout.CENTER);
		
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		addWindowListener(windowListener);
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (screen.width/2) - (StyleConstants.WINDOW_WIDTH/2);
		int y = (screen.height/2) - (StyleConstants.WINDOW_HEIGHT/2);
		this.setMinimumSize(new Dimension(StyleConstants.WINDOW_WIDTH, StyleConstants.WINDOW_HEIGHT));
		this.setBounds(x, y, StyleConstants.WINDOW_WIDTH, StyleConstants.WINDOW_HEIGHT);
		this.setResizable(false);
		this.setVisible(true);
	}
	
	public void switchPage(JComponent component) {
		currComponent = component;
    	bodyPanel.removeAll();
		bodyPanel.add(component, BorderLayout.NORTH);
		bodyPanel.getRootPane().revalidate();
		this.getContentPane().repaint();
    }
	
	public Object getFormData() {
		if(currComponent instanceof AddJobAdvertPanel)
			return ((AddJobAdvertPanel)currComponent).getFormData();
		
		return null;
	}
	
	public void clearFormData() {
		if(currComponent instanceof AddJobAdvertPanel)
			((AddJobAdvertPanel)currComponent).clearFormData();
	}
	
	private JPanel initHeaderPanel() {
		JPanel menuPanel = new JPanel();
		menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		menuPanel.add(new ToolbarMenu(mainMenuController));
		
		JPanel headerPanel = new JPanel();
		headerPanel.setLayout(new BorderLayout());
		headerPanel.add(menuPanel, BorderLayout.CENTER);
		
		return headerPanel;
	}
	
	private JPanel initBodyPanel() {
		JLabel msgLabel = new JLabel("Job Advertiser Main Menu");
		msgLabel.setFont(new Font("Calibri", Font.BOLD, 32));
		msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
		
		JPanel framePanel = new JPanel();
		framePanel.setLayout(new BorderLayout());
		framePanel.add(msgLabel, BorderLayout.CENTER);
		
		return framePanel;
	}
	
	private WindowListener windowListener = new WindowAdapter () {
        public void windowClosing (WindowEvent e) {
        	mainMenuController.exitProgram();
        }
    };
}
